package com.literature.search.file.controller;

import com.literature.search.core.common.MessageConstant;
import com.literature.search.core.entity.R.AjaxJson;
import com.literature.search.core.utils.so.SoMap;
import com.literature.search.core.utils.sp.SpUserUtil;
import com.literature.search.file.entity.vo.CollectionListVO;
import com.literature.search.file.entity.vo.LikeListVO;
import com.literature.search.file.service.IFileService;
import com.literature.search.file.service.LikeAndCollectionService;
import io.lettuce.core.RedisConnectionException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName LikeAndCollectionController
 * @Description TODO
 * @Author abin
 * @Date 2023/3/24 17:46
 * @Version 1.0
 */
@Api(tags = "点赞收藏管理")
@RestController
@RequestMapping("/")
public class LikeAndCollectionController {
    @Resource
    private LikeAndCollectionService likeAndCollectionService;

    @Resource
    private IFileService fileService;

    @PostMapping("like")
    @ApiOperation(value = "点赞",notes = "用户对文件进行点赞")
    public AjaxJson like(@RequestParam String fileId){
        String userId = SpUserUtil.getCurrUser().getId();
        long Count = 0;
        int Status = 0;
        try {
            // 点赞
            boolean status = likeAndCollectionService.like(fileId, userId);
            if (status){
                // 获取点赞的数量
                Count = likeAndCollectionService.getLikedCount(fileId);
                // 获取当前用户点赞的状态
                Status = likeAndCollectionService.getLikeStatus(fileId,userId);
            }
            else {
                return AjaxJson.getError("没有建立ES索引");
            }
        } catch (RedisConnectionFailureException | RedisConnectionException e) {
            return AjaxJson.getError(e.getMessage());
        }
        SoMap map = new SoMap();
        map.put("Count", Count);
        map.put("Status", Status);
        return AjaxJson.getSuccessData(map);
    }

    @PostMapping("dislike")
    @ApiOperation(value = "取消点赞",notes = "用户取消对文件的点赞")
    public AjaxJson dislike(@RequestParam String fileId){
        String userId = SpUserUtil.getCurrUser().getId();
        long Count = 0;
        int Status = 0;
        try {
            // 取消点赞
            boolean status = likeAndCollectionService.dislike(fileId,userId);
            if(status) {
                // 获取点赞的数量
                Count = likeAndCollectionService.getLikedCount(fileId);
                // 获取当前用户点赞的状态
                Status = likeAndCollectionService.getLikeStatus(fileId, userId);
            }
            else {
                return AjaxJson.getError("没有建立ES索引");
            }
        } catch (RedisConnectionFailureException | RedisConnectionException e) {
            return AjaxJson.getError(e.getMessage());
        }
        SoMap map = new SoMap();
        map.put("Count", Count);
        map.put("Status", Status);
        return AjaxJson.getSuccessData(map);
    }

    @PostMapping("collect")
    @ApiOperation(value = "收藏",notes = "用户对文件进行收藏")
    public AjaxJson collect(@RequestParam String fileId){
        String userId = SpUserUtil.getCurrUser().getId();
        long Count = 0;
        int Status = 0;
        try {
            // 收藏
            boolean status = likeAndCollectionService.collect(fileId, userId);
            if(status) {
                // 获取收藏的数量
                Count = likeAndCollectionService.getCollectedCount(fileId);
                // 获取当前用户收藏的状态
                Status = likeAndCollectionService.getCollectStatus(fileId, userId);
            }
            else {
                return AjaxJson.getError("没有建立ES索引");
            }
        } catch (RedisConnectionFailureException | RedisConnectionException e) {
            return AjaxJson.getError(e.getMessage());
        }
        SoMap map = new SoMap();
        map.put("Count", Count);
        map.put("Status", Status);
        return AjaxJson.getSuccessData(map);
    }

    @PostMapping("cancelCollect")
    @ApiOperation(value = "取消收藏",notes = "用户取消对文件的收藏")
    public AjaxJson cancelCollect(@RequestParam String fileId){
        String userId = SpUserUtil.getCurrUser().getId();
        long Count = 0;
        int Status = 0;
        try {
            // 取消收藏
            boolean status = likeAndCollectionService.cancelCollect(fileId,userId);
            if (status) {
                // 获取收藏的数量
                Count = likeAndCollectionService.getCollectedCount(fileId);
                // 获取当前用户收藏的状态
                Status = likeAndCollectionService.getCollectStatus(fileId, userId);
            }
            else{
                return AjaxJson.getError("没有建立ES索引");
            }
        } catch (RedisConnectionFailureException | RedisConnectionException e) {
            return AjaxJson.getError(e.getMessage());
        }
        SoMap map = new SoMap();
        map.put("Count", Count);
        map.put("Status", Status);
        return AjaxJson.getSuccessData(map);
    }

    @ApiOperation(value = "获取用户的点赞列表",notes = "用户获取自己的点赞列表")
    @GetMapping("myLikeList")
    public AjaxJson queryUserLikeList(){
        List<LikeListVO> likeListVOList = likeAndCollectionService.queryUserLikeList();
        Integer count = likeListVOList.size();
        SoMap map = new SoMap();
        map.put("likeList",likeListVOList);
        map.put("count",count);
        return AjaxJson.getSuccessData(map);
    }

    @ApiOperation(value = "获取用户的收藏列表",notes = "用户获取自己的收藏列表")
    @GetMapping("myCollectionList")
    public AjaxJson queryUserCollectionList(){
        List<CollectionListVO> collectionListVOList = likeAndCollectionService.queryUserCollectionList();
        Integer count = collectionListVOList.size();
        SoMap map = new SoMap();
        map.put("collectionList",collectionListVOList);
        map.put("count",count);
        return AjaxJson.getSuccessData(map);
    }

    @ApiOperation(value = "获取缩略图",notes = "获取文件的首页缩略图")
    @GetMapping("/thumb/{ThumbId}")
    public ResponseEntity<Object> previewThumb(@PathVariable String ThumbId)  {
        if (StringUtils.hasText(ThumbId)) {
            byte[] bytes = fileService.getFileThumb(ThumbId);
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "fileName=" + ThumbId)
                    .header(HttpHeaders.CONTENT_TYPE, "image/png")
                    .header(HttpHeaders.CONTENT_LENGTH, bytes.length + "")
                    .body(bytes);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(MessageConstant.FILE_NOT_FOUND);
        }
    }
}
